<template>
  <div class="mod-config">
    <el-form
      :inline="true"
      :model="dataForm"
      ref="dataFormA"
      @keyup.enter.native="getDataList()"
    >
      <el-form-item label="分类" prop="categoryId">
        <el-cascader
          v-model="dataForm.categoryPath"
          :options="options"
          @change="handleChange"
          placeholder="请选择分类"
          clearable
        >
        </el-cascader>
      </el-form-item>
      <el-form-item label="课程" prop="courseId">
        <el-select
          v-model="dataForm.courseId"
          placeholder="请选择课程"
          clearable
        >
          <el-option
            v-for="item in courses"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="类型" prop="type">
        <el-select v-model="dataForm.type" placeholder="请选择类型" clearable>
          <el-option
            v-for="item in types"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="录入人">
        <el-input v-model="dataForm.input" placeholder="录入人" clearable>
        </el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="getDataList()">查询</el-button>
        <el-button
          v-if="isAuth('web:testquestion:save')"
          type="primary"
          @click="addOrUpdateHandle()"
          >新增</el-button
        >
        <el-button
          v-if="isAuth('web:testquestion:delete')"
          type="danger"
          @click="deleteHandle()"
          :disabled="dataListSelections.length <= 0"
          >批量删除</el-button
        >
      </el-form-item>
    </el-form>
    <el-table
      :data="dataList"
      border
      v-loading="dataListLoading"
      @selection-change="selectionChangeHandle"
      style="width: 100%;"
    >
      <el-table-column
        type="selection"
        header-align="center"
        align="center"
        width="50"
      >
      </el-table-column>
      <!-- <el-table-column
        prop="id"
        header-align="center"
        align="center"
        label="id">
      </el-table-column> -->
      <el-table-column
        prop="categoryName"
        header-align="center"
        align="center"
        label="分类"
      >
      </el-table-column>
      <el-table-column
        prop="courseName"
        header-align="center"
        align="center"
        label="课程"
      >
      </el-table-column>
      <el-table-column
        prop="type"
        header-align="center"
        align="center"
        label="类型"
      >
      </el-table-column>
      <el-table-column
        prop="question"
        header-align="center"
        align="center"
        label="题目"
      >
        <template slot-scope="scope">
          <p v-html="scope.row.question"></p>
        </template>
      </el-table-column>
      <el-table-column
        prop="answer"
        header-align="center"
        align="center"
        label="答案"
      >
        <template slot-scope="scope">
          <p v-html="scope.row.answer"></p>
        </template>
      </el-table-column>
      <el-table-column
        prop="score"
        header-align="center"
        align="center"
        label="分数"
      >
      </el-table-column>
      <el-table-column
        prop="input"
        header-align="center"
        align="center"
        label="录入人"
      >
      </el-table-column>
      <el-table-column
        prop="createTime"
        header-align="center"
        align="center"
        label="录入时间"
      >
      </el-table-column>
      <!-- <el-table-column
        prop="updateTime"
        header-align="center"
        align="center"
        label="修改时间">
      </el-table-column> -->
      <el-table-column
        fixed="right"
        header-align="center"
        align="center"
        width="150"
        label="操作"
      >
        <template slot-scope="scope">
          <el-button
            type="text"
            size="small"
            @click="addOrUpdateHandle(scope.row.id)"
            >修改</el-button
          >
          <el-button
            type="text"
            size="small"
            @click="deleteHandle(scope.row.id)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper"
    >
    </el-pagination>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update
      v-if="addOrUpdateVisible"
      ref="addOrUpdate"
      @refreshDataList="getDataList"
    ></add-or-update>
  </div>
</template>

<script>
import AddOrUpdate from "./testquestion-add-or-update";
export default {
  data() {
    return {
      dataForm: {
        categoryPath: [],
        categoryId: "",
        courseId: "",
        type: "",
        input: ""
      },
      options: [],
      types: [
        {
          value: "选择",
          label: "选择"
        },
        {
          value: "填空",
          label: "填空"
        },
        {
          value: "问答",
          label: "问答"
        }
      ],
      courses: [],
      dataList: [],
      pageIndex: 1,
      pageSize: 10,
      totalPage: 0,
      dataListLoading: false,
      dataListSelections: [],
      addOrUpdateVisible: false
    };
  },
  components: {
    AddOrUpdate
  },
  activated() {
    this.getDataList();
  },
  methods: {
    // 获取数据列表
    getDataList() {
      this.dataListLoading = true;
      this.$http({
        url: this.$http.adornUrl("/web/testquestion/list"),
        method: "get",
        params: this.$http.adornParams({
          page: this.pageIndex,
          limit: this.pageSize,
          categoryId: this.dataForm.categoryId,
          courseId: this.dataForm.courseId,
          type: this.dataForm.type,
          input: this.dataForm.input
        })
      }).then(({ data }) => {
        // this.dataForm.categoryId = "";
        // this.dataForm.categoryPath = [];
        // this.options = [];
        // this.dataForm.courseId = "";
        // this.dataForm.type = "";
        // this.dataForm.input = "";
        // this.courses = [];
        if (data && data.code === 0) {
          this.dataList = data.page.list;
          this.totalPage = data.page.totalCount;
        } else {
          this.dataList = [];
          this.totalPage = 0;
        }
        this.dataListLoading = false;
      });
      // 获取分类信息
      this.$http({
        url: this.$http.adornUrl(`/web/category/get/category/list`),
        method: "get"
      }).then(({ data }) => {
        if (data && data.code === 0) {
          let res = data.data;
          let d = [];
          res.forEach(item => {
            let model = {};
            model.label = item.name;
            model.value = item.id;
            if (item.children.length > 0) {
              this.formatCategoryPath(model, item.children);
            }
            d.push(model);
          });
          this.options = d;
        }
      });
    },
    // 每页数
    sizeChangeHandle(val) {
      this.pageSize = val;
      this.pageIndex = 1;
      this.getDataList();
    },
    // 当前页
    currentChangeHandle(val) {
      this.pageIndex = val;
      this.getDataList();
    },
    // 多选
    selectionChangeHandle(val) {
      this.dataListSelections = val;
    },
    // 新增 / 修改
    addOrUpdateHandle(id) {
      this.addOrUpdateVisible = true;
      setTimeout(() => {
        this.$nextTick(() => {
          this.$refs.addOrUpdate.init(id);
        });
      }, 500);
    },
    // 删除
    deleteHandle(id) {
      var ids = id
        ? [id]
        : this.dataListSelections.map(item => {
            return item.id;
          });
      this.$confirm(`确定对进行${id ? "删除" : "批量删除"}操作?`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        this.$http({
          url: this.$http.adornUrl("/web/testquestion/delete"),
          method: "post",
          data: this.$http.adornData(ids, false)
        }).then(({ data }) => {
          if (data && data.code === 0) {
            this.$message({
              message: "操作成功",
              type: "success",
              duration: 1500,
              onClose: () => {
                this.getDataList();
              }
            });
          } else {
            this.$message.error(data.msg);
          }
        });
      });
    },
    formatCategoryPath(parent, children) {
      let d = [];
      children.forEach(item => {
        let model = {};
        model.label = item.name;
        model.value = item.id;
        if (item.children.length > 0) {
          this.formatCategoryPath(model, item.children);
        }
        d.push(model);
      });
      parent.children = d;
    },
    handleChange(value) {
      this.dataForm.categoryId = value[value.length - 1];
      this.dataForm.courseId = "";
      // 获取课程信息
      this.courses = [];
      let categoryId = this.dataForm.categoryId;
      if (categoryId != null && categoryId !== "" && categoryId !== 0) {
        this.$http({
          url: this.$http.adornUrl(`/web/course/get/list/by/${categoryId}`),
          method: "get"
        }).then(({ data }) => {
          let courses = data.data;

          courses.forEach(item => {
            let course = {
              value: item.id,
              label: item.name
            };
            this.courses.push(course);
          });
        });
      }
    }
  }
};
</script>
